//
//  ZCMVViewController.m
//  VoiceMusic
//
//  Created by dllo on 15/10/16.
//  Copyright © 2015年 zhangdeyu. All rights reserved.
//

#import "ZCMVViewController.h"
#import <Masonry.h>
#import "ZCMVUpdateViewController.h"
#import "ZCMVClassifyViewController.h"
@interface ZCMVViewController ()<UIScrollViewDelegate>

@end

@implementation ZCMVViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    _buttonArray = [[NSMutableArray alloc] init];
    [self.view setBackgroundColor:[UIColor whiteColor]];
    [self creatView];
    // Do any additional setup after loading the view.
    //返回键
    UIButton *leftButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [leftButton setImage:[UIImage imageNamed:@"iconfont-fanhui"] forState:UIControlStateNormal];
    leftButton.frame = CGRectMake(0, 0, 30, 30);
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:leftButton];
    [leftButton addTarget:self action:@selector(returnClick:) forControlEvents:UIControlEventTouchUpInside];
}
#pragma mark 返回
- (void)returnClick:(UIBarButtonItem *)button
{
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)creatView
{
    
    CGFloat xlen = self.view.frame.size.width;
    CGFloat ylen = self.view.frame.size.height;
    
    _headlineScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 50)];
    [_headlineScrollView setDelegate:self];
    [self setAutomaticallyAdjustsScrollViewInsets:NO];
    [_headlineScrollView setContentSize:CGSizeMake(0, 0)];
    [self.view addSubview:_headlineScrollView];
    
    _array = [NSMutableArray arrayWithObjects:@"MV分类", @"每日一发", nil];
    for (int i = 0; i < 2; i++) {
        _button = [UIButton buttonWithType:UIButtonTypeCustom];
        [_button setFrame:CGRectMake(i * xlen / 2, 0, xlen / 2, 50)];
        [_button setTitle:_array[i] forState:UIControlStateNormal];
        [_button setTag:i];
        [_button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [_button setTitleColor:[UIColor blueColor] forState:UIControlStateSelected];
        [_button addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
        [_headlineScrollView addSubview:_button];
        //创建一个可变数组把button装进去
        [_buttonArray addObject:_button];
    }
    
    _contentScrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 50, self.view.frame.size.width, self.view.frame.size.height - 114)];
    [_contentScrollView setContentSize:CGSizeMake(0, 0)];
    _contentScrollView.clipsToBounds = NO;
    [_contentScrollView setPagingEnabled:YES];
    [_contentScrollView setBackgroundColor:[UIColor grayColor]];
    [_contentScrollView setTag:1001];
    [_contentScrollView setDelegate:self];
    [self.view addSubview:_contentScrollView];
    
    for (int i = 0; i < 2; i++) {
        self.pictureView  = [[UIView alloc]initWithFrame:CGRectMake(xlen * i, 0, xlen, ylen * 2)];
        [self.pictureView setBackgroundColor:[UIColor whiteColor]];
        [self.pictureView setTag:i];
        [_contentScrollView addSubview:self.pictureView];
    }
    
    ZCMVUpdateViewController * hotVC = [[ZCMVUpdateViewController alloc]init];
    hotVC.view.frame = CGRectMake(xlen, 0, xlen, ylen);
    [_contentScrollView addSubview:hotVC.view];
    [self addChildViewController:hotVC];
    
    ZCMVClassifyViewController * hotVC1 = [[ZCMVClassifyViewController alloc] init];
    hotVC1.view.frame = CGRectMake(0, 0, xlen, ylen);
    [_contentScrollView addSubview:hotVC1.view];
    [self addChildViewController:hotVC1];
    
}

- (void)buttonClick:(UIButton *)sender {
    [_contentScrollView  setContentOffset:CGPointMake(self.view.frame.size.width * sender.tag, 0)];
    
    if (sender.selected == NO) {
        sender.selected = YES;
        
    }
    for (UIButton * obj in _buttonArray) {
        if (obj.selected == YES && obj.tag != sender.tag) {
            obj.selected = NO;
            
        }
    }
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    for (UIButton * btn in _buttonArray) {
        if (btn.tag == scrollView.contentOffset.x / self.view.frame.size.width) {
            if (btn.selected == NO) {
                btn.selected = YES;
            }
            
            for (UIButton * obj in _buttonArray) {
                if (obj.selected == YES && obj.tag != btn.tag) {
                    obj.selected = NO;
                }
            }
        }
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
